package com.alon.spring01.study.async;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.Locale;
import java.util.concurrent.RecursiveTask;

/**
 * @program: spring01
 * @description: 使用fork/Join拆分大任务
 * @author: yulongguang
 * @create: 2019-09-27 20:20
 **/
public class LocalDataTimeTest extends RecursiveTask<Long> {

    private static ThreadLocal<SimpleDateFormat> local = new ThreadLocal<>();

    /**
     * 字符串Str转化成Date
     *
     * @param str
     * @return
     * @throws ParseException
     */
    public static Date parse(String str) throws ParseException {
        SimpleDateFormat simpleDateFormat = local.get();
        if (simpleDateFormat == null) {
            simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:MM:ss",Locale.CHINESE);
            local.set(simpleDateFormat);
        }
        return simpleDateFormat.parse(str);
    }

    /**
     * date 转字符串
     *
     * @param date
     * @return
     */
    private static String format(Date date) {
        SimpleDateFormat simpleDateFormat = local.get();
        if (simpleDateFormat == null) {
            simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:MM:ss", Locale.CHINESE);
            local.set(simpleDateFormat);
        }
        return simpleDateFormat.format(date);
    }

//    public static void main(String[] args) throws ParseException {
//
//        String format = format(new Date());
//        System.out.println(format);
//        Date parse = parse(format);
//        System.out.println(parse);
//    }

    @Override
    protected Long compute() {
        return null;
    }

    public static void main(String[] args) {
        // LocalDateTime 转字符串
        String format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS").format(LocalDateTime.now());
        System.out.println(format);
        String format1 = DateTimeFormatter.ofPattern("yyyy-MM-dd").format(LocalDate.now());
        System.out.println(format1);
        //字符串转localDateTime 和localDate
//        LocalDateTime parse = LocalDateTime.parse("2019-09-29 16:50:55.997", DateTimeFormatter.ofPattern("YYYY-MM-dd HH:mm:ss.SSS"));
        LocalDateTime localDateTime = LocalDateTime.parse("2019-05-14 21:15:30.555", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"));
        System.out.println(localDateTime);
        //时间格式化
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate date = LocalDate.parse("2017-06-17", formatter);
        String formatDate = formatter.format(date);
        System.out.println(formatDate);
        //LocalDate  转date
        LocalDate localDate = LocalDate.now();
        ZoneId zone = ZoneId.systemDefault();
        Instant instant = localDate.atStartOfDay().atZone(zone).toInstant();
        Date from = Date.from(instant);
        System.out.println(from);
        //LocalDate 转Date

        Date from2 = Date.from(LocalDate.now().atStartOfDay(ZoneId.systemDefault()).toInstant());
        System.out.println(from2);
        //LocalTime   转date
        LocalTime localTime = LocalTime.now();
        LocalDate localDate1 = LocalDate.now();
        LocalDateTime localDateTime1 = LocalDateTime.of(localDate1, localTime);
        //时间戳
        Instant toInstant = localDateTime1.atZone(ZoneId.systemDefault()).toInstant();
        System.out.println(Date.from(toInstant));

        //LocalDateTime 转date
        Date from1 = Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant());
        System.out.println(from1);
        //date 转LocalTime
        LocalTime toLocalTime = LocalDateTime.ofInstant(new Date().toInstant(), ZoneId.systemDefault()).toLocalTime();
        System.out.println(toLocalTime.format(DateTimeFormatter.ofPattern("HH:mm:ss")));
        //date 转LocalDate
        LocalDate toLocalDate = LocalDateTime.ofInstant(new Date().toInstant(), ZoneId.systemDefault()).toLocalDate();
        String format2 = toLocalDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        System.out.println(format2);
        //date 转LocalDateTime
        LocalDateTime localDateTime2 = LocalDateTime.ofInstant(new Date().toInstant(), ZoneId.systemDefault());
        System.out.println("年：" + localDateTime2.getYear() + "月：" + localDateTime2.getMonth() + "日：" + localDateTime2.getDayOfMonth() + "时：" + localDateTime2.getHour() +
                "分：" + localDateTime2.getMinute() +
                "秒：" + localDateTime2.getSecond());
        System.out.println(localDateTime2.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

        LocalDate parse = LocalDate.parse(format2);
        System.out.println(parse);
        //时间戳
        Instant.now();
        //获取毫秒值
        Instant.now().toEpochMilli();


        LocalDate localDate2 = LocalDate.parse("2019-09-17", formatter);
        LocalDate localDate3 = LocalDate.parse("2019-09-29", formatter);
        //两个日期相差天数
        long between = ChronoUnit.DAYS.between(localDate2, localDate3);
        System.out.println(between);

        DateTimeFormatter formatter01 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime localDateTime3 = LocalDateTime.parse("2019-09-17 21:15:30", formatter01);
        LocalDateTime localDateTime4 = LocalDateTime.parse("2019-09-29 21:15:30", formatter01);
        long between1 = ChronoUnit.DAYS.between(localDateTime3, localDateTime4);
        System.out.println(between1);

    }
}
